如何返回<div>的属性的值

来源:百度知道 编辑:UC知道 时间:2024/05/26 09:24:28
<div>设置了CSS后,怎么用javascritp的alert弹出<div>的属性值,如高,宽,颜色,文本等,这样写怎么读不出来?
var div=document.getElementById(a1);
alert(a1.style.width);
恩,我试过了用行内定义的样式可以显示,但是用内嵌式定义的就显示不出(就是在头部用<style type="text/css">统一定义),奇怪啊,能解决吗?

可以显示,例子如下:

<div id=a1 style="width:100;color:red">啦啦啦</div>

<script language=javascript>
alert('color:'+a1.style.color+', width:'+a1.style.width);
</script>

说明,对于从来没有设置过的,使用默认值的属性,显示结果为空。

<style type="text/css">
#a1{width:100px; height:100px;}
</style>

<div id="a1">测试</div>

<script language=javascript>
alert('宽:'+a1.offsetWidth+',高:'+a1.offsetHeight+',文字:'+a1.innerHTML);
</script>